package com.shop.dao.impl;

import com.shop.dao.GoodsDao;
import com.shop.dao.base.BaseDao;
import com.shop.entity.Goods;
import com.shop.entity.VO.GoodsParamVo;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/3/11 15:10
 **/
public class GoodsDaoImpl extends BaseDao<Goods> implements GoodsDao {
	@Override
	public List<Goods> findGoodByPage(int beginIndex, int showNum) {
		String sql = "SELECT\n" +
				"g.id,\n" +
				"g.goods_name AS goodsName,\n" +
				"g.goods_desc AS goodsDesc,\n" +
				"g.price,\n" +
				"g.stock,\n" +
				"g.classify_level1_id AS classifyLevel1Id,\n" +
				"g.classify_level2_id AS classifyLevel2Id,\n" +
				"g.classify_level3_id AS classifyLevel3Id,\n" +
				"g.file_name AS fileName,\n" +
				"g.is_delete\n" +
				"FROM\n" +
				"t_goods AS g\n" +
				"LIMIT ?, ?";
		Object[] params = {beginIndex,showNum};
		try {
			return super.getResultList(sql, new Goods(), params);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Goods> findGoodByPage(GoodsParamVo paramVo) {
		String sql = "SELECT\n" +
			"g.id,\n" +
			"g.goods_name AS goodsName,\n" +
			"g.goods_desc AS goodsDesc,\n" +
			"g.price,\n" +
			"g.stock,\n" +
			"g.classify_level1_id AS classifyLevel1Id,\n" +
			"g.classify_level2_id AS classifyLevel2Id,\n" +
			"g.classify_level3_id AS classifyLevel3Id,\n" +
			"g.file_name AS fileName,\n" +
			"g.is_delete\n" +
			" FROM\n" +
			" t_goods AS g \n" +
			" where 1=1";
		//分类
		if(paramVo!=null && paramVo.getLevel() == 1){
			sql += " and g.classify_level1_id = "+paramVo.getId();
		}else if(paramVo!=null && paramVo.getLevel() == 2){
			sql += " and g.classify_level2_id = "+paramVo.getId();
		}else if(paramVo!=null && paramVo.getLevel() == 3){
			sql += " and g.classify_level3_id = "+paramVo.getId();
		}
//		//商品名称
//		String goodsName = "糖";
//		if(!goodsName.equals("")){
//			sql += " and g.goods_name like '%"+goodsName+"%' ";
//		}
//		//商品价格
//		Integer beginGoodsPrice = 100;
//		Integer endGoodsPrice = 500;
//		if(beginGoodsPrice!=null && endGoodsPrice!=null){
//			sql += " and g.price>="+beginGoodsPrice+" and g.price<="+endGoodsPrice+"";
//		}else if(beginGoodsPrice!=null ){
//			sql += " and g.price>="+beginGoodsPrice;
//		}else if(endGoodsPrice!=null ){
//			sql += " and g.price<="+endGoodsPrice;
//		}

		sql += " LIMIT ?, ?";
		Object[] params = {paramVo.getBeginIndex(),paramVo.getShowNum()};
		try {
			return super.getResultList(sql, new Goods(), params);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return null;
	}

	@Override
	public Goods findById(Integer goodsId) {
		String sql = "SELECT\n" +
				"g.id,\n" +
				"g.goods_name AS goodsName,\n" +
				"g.goods_desc AS goodsDesc,\n" +
				"g.price,\n" +
				"g.stock,\n" +
				"g.classify_level1_id AS classifyLevel1Id,\n" +
				"g.classify_level2_id AS classifyLevel2Id,\n" +
				"g.classify_level3_id AS classifyLevel3Id,\n" +
				"g.file_name AS fileName,\n" +
				"g.is_delete\n" +
				"FROM\n" +
				"t_goods AS g\n" +
				"where g.id=?";

		Object[] params = {goodsId};
		try {
			return super.getResult(sql, new Goods(), params);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return null;
	}

	@Override
	public int modifyStock(Integer goodsId, Integer stock) {
		String sql = "update t_goods set stock=? where id=?";
		Integer[] params ={stock,goodsId};
		try {
			return super.executeUpdate(sql,params);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		return 0;
	}
}
